using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class RelatedSprite : MonoBehaviour
{
	[Header("This sprites and colors setting will be follow with this Ref.")]
	public RelatedSprite refSprites;

	public RelatedSprite refColors;

	[Header("This sprites setting")]
	public Sprite[] sprites;

	[Header("Sprites For Matching")]
	public RelatedSprite refSpritesForMatching;

	public Sprite[] spritesForMatching;

	public Sprite[] spritesForMatched;

	[Header("This colors setting")]
	public Gradient colors;

	[Range(0f, 1f)]
	public float darkRatio;

	private bool spriteReady;

	private bool colorReady;

	private SpriteRenderer spriteRenderer;

	private void Awake()
	{
		spriteRenderer = GetComponent<SpriteRenderer>();
	}

	private void Start()
	{
		SetSprite();
		SetColor();
		Object.Destroy(this);
	}

	public void SetSprite()
	{
		if (spriteReady)
		{
			return;
		}
		if ((bool)refSpritesForMatching)
		{
			refSpritesForMatching.SetSprite();
		}
		bool flag = false;
		if ((bool)refSprites)
		{
			refSprites.SetSprite();
			spriteRenderer.sprite = refSprites.spriteRenderer.sprite;
		}
		else if (spritesForMatching.Length != 0)
		{
			Sprite[] array = spritesForMatching;
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i] == refSpritesForMatching.spriteRenderer.sprite)
				{
					if (spritesForMatched.Length != 0)
					{
						spriteRenderer.sprite = spritesForMatched[Random.Range(0, spritesForMatched.Length)];
						flag = true;
					}
					break;
				}
			}
		}
		if (!flag && sprites.Length != 0)
		{
			spriteRenderer.sprite = sprites[Random.Range(0, sprites.Length)];
		}
		spriteReady = true;
	}

	public void SetColor()
	{
		if (!colorReady)
		{
			if ((bool)refColors)
			{
				refColors.SetColor();
				spriteRenderer.color = GetColor(refColors.spriteRenderer.color);
			}
			else
			{
				spriteRenderer.color = GetColor(colors.Evaluate(Random.Range(0f, 1f)));
			}
			colorReady = true;
		}
	}

	public Color GetColor(Color color)
	{
		if (darkRatio != 0f)
		{
			color = Color.Lerp(color, Color.black, darkRatio);
		}
		return color;
	}
}
